﻿using SixLabors.ImageSharp;
using SixLabors.Fonts;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SixLabors.ImageSharp.Drawing.Processing;

namespace Infrastructure.Util.Helper
{
    public class VCodeHelper
    {
        private static readonly Color[] Colors = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Brown, Color.Purple };
        private static readonly char[] Chars = {
             '2', '3', '4', '5', '6', '7', '8', '9',
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm', 'n',  'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',  'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
        };


        public static (string code, MemoryStream bytes) GenVCode(int Width, int Height, int num = 6)
        {
            var code = GenCode(num);
            var r = new Random();

            int fontHeight = Width / num;

            using var image = new Image<Rgba32>(Width, Height);
            var font = SystemFonts.CreateFont(SystemFonts.Families.First().Name, fontHeight, FontStyle.Bold);

            image.Mutate(ctx =>
            {
                ctx.Fill(Color.White);

                for (int i = 0; i < code.Length; i++)
                {
                    var aa = r.Next(-15, 15);
                    using var image2 = new Image<Rgba32>(fontHeight, fontHeight);
                    image2.Mutate(x => x.Rotate(aa).DrawText(code[i].ToString(), font, Colors[r.Next(Colors.Length)], new PointF(2, 2)).Rotate(-aa));

                    var aaa = r.Next(-fontHeight, fontHeight);

                    var x = fontHeight * i + (i == 0 ? 0 : aaa / 4);

                    x = x < 0 ? 1 : x;
                    x = x > Width - fontHeight ? Width - fontHeight : x;

                    ctx.DrawImage(image2, new Point(x, r.Next(Height - fontHeight - 2)), 1);
                }
               
                for (int i = 0; i < 10; i++)
                {
                    var pen = new SolidPen(Colors[r.Next(Colors.Length)],1);
                    var p1 = new PointF(r.Next(Width), r.Next(Height));
                    var p2 = new PointF(r.Next(Width), r.Next(Height));

                    ctx.DrawLine(pen, p1, p2);
                }

                for (int i = 0; i < 80; i++)
                {
                    var pen = new SolidPen(Colors[r.Next(Colors.Length)], 1);
                    var p1 = new PointF(r.Next(Width), r.Next(Height));
                    var p2 = new PointF(p1.X + 1f, p1.Y + 1f);

                    ctx.DrawLine(pen, p1, p2);
                }
            });

            using var ms = new System.IO.MemoryStream();
            image.SaveAsGif(ms);
            return (code, ms);
        }

        private static string GenCode(int num)
        {
            var code = string.Empty;
            var r = new Random();

            for (int i = 0; i < num; i++)
            {
                code += Chars[r.Next(Chars.Length)].ToString();
            }

            return code;
        }
    }

}
